/**
 * 
 */
package org.compiere.model;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Properties;
import java.util.logging.Level;

import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.Env;

/**
 * @author A.Yerzhanov
 * 28.06.2013
 */
public class Mhrmcharge extends X_hrm_charge {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 8422202482813827338L;
	
	/**	Static Logger	*/
	private static CLogger	s_log	= CLogger.getCLogger (Mhrmcharge.class);

	/**
	 * @param ctx
	 * @param hrm_charge_ID
	 * @param trxName
	 */
	public Mhrmcharge(Properties ctx, int hrm_charge_ID, String trxName) {
		super(ctx, hrm_charge_ID, trxName);
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param ctx
	 * @param rs
	 * @param trxName
	 */
	public Mhrmcharge(Properties ctx, ResultSet rs, String trxName) {
		super(ctx, rs, trxName);
		// TODO Auto-generated constructor stub
	}

	public static Mhrmcharge getCharge(String chargetype, Timestamp dt) {
		//
	    PreparedStatement pstmt = null;
		ResultSet rs = null;
		Mhrmcharge result = null;
			
		// 
		String sql_ = "SELECT * FROM HRM_CHARGE \n" +
						"WHERE ISACTIVE='Y' \n"  +
							" AND AD_CLIENT_ID="  + Env.getAD_Client_ID(Env.getCtx()) + " \n" +
							" AND HRM_CHARGETYPE='" + chargetype + "' \n" +
							" AND " + DB.TO_DATE(dt, true) + " BETWEEN STARTDATE AND ENDDATE";
			try
			{
				pstmt = DB.prepareStatement(sql_,null);
				rs = pstmt.executeQuery();	
				if(rs.next())
					result = new Mhrmcharge(Env.getCtx(), rs, null);
			}
			catch (SQLException e)
			{
				s_log.log(Level.SEVERE, "product", e);
			}
			finally
			{
				DB.close(rs, pstmt);
				rs = null; pstmt = null;
			}
			
		return result;		
	}
	
	public static int getChargeID(String chargetype, Timestamp dt) {
		//
	    PreparedStatement pstmt = null;
		ResultSet rs = null;
		int result = 0;
			
		// 
		String sql_ = "SELECT C_CHARGE_ID FROM HRM_CHARGE \n" +
						"WHERE ISACTIVE='Y' \n"  +
							" AND AD_CLIENT_ID="  + Env.getAD_Client_ID(Env.getCtx()) + " \n" +
							" AND HRM_CHARGETYPE='" + chargetype + "' \n" +
							" AND " + DB.TO_DATE(dt, true) + " BETWEEN STARTDATE AND ENDDATE";
			try
			{
				pstmt = DB.prepareStatement(sql_,null);
				rs = pstmt.executeQuery();	
				if(rs.next())
					result = rs.getInt(1);
			}
			catch (SQLException e)
			{
				s_log.log(Level.SEVERE, "product", e);
			}
			finally
			{
				DB.close(rs, pstmt);
				rs = null; pstmt = null;
			}
			
		return result;		
	}
}
